package com.it2297.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Timestamp;

import javax.swing.Box;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import com.it2297.controller.JobController;
import com.it2297.main.CompanyApp;

import javax.swing.JFormattedTextField;

public class JobCreateJob extends JFrame{

	private JMenuBar menuBar;
	private JMenu menuHome;
	private JMenu menuExit;
	private JMenu menuJobAd;
	private JMenuItem menuRemoveJob;
	private JMenuItem menuCreateJob;
	private JMenuItem menuViewAllJobsByCompany;
	private JMenu menuApplicants;
	private JMenuItem menuViewApplicants;
	private JSeparator separator;
	private Component horizontalGlue;
	private Component horizontalStrut;
	private JLabel lblWelcome;
	private JMenuItem menuLogout;
	private JMenuItem menuhomescreen;
	private JMenu mnProfile;
    private JMenuItem mntmUpdateCompany;
	
	
	
	
	private JPanel contentPane;
	private final JLabel lblNewLabel = new JLabel("Post Job Advertisement");
	private JTextField tf_title;
	private JTextField tf_desc;
	private JTextField tf_req;
	private JTextField tf_duty;
	private JTextField tf_it;
	private JComboBox cb_et;
	private JSpinner wst;
	private JComboBox cb_wst;
	private JSpinner wet;
	private JComboBox cb_wet;
	private JTextField tf_benefit;
	private JTextField tf_cname;
	final JobController jct= new JobController(this);
	private JSpinner salary;
	private JTextField tf_salary;

	
	 public String getTitle(){
         return tf_title.getText();
	 }
	public String getDesc(){
		return tf_desc.getText();
	}
	public String getReq(){
		return tf_req.getText();
	}
	public String getDuty(){
		return tf_duty.getText();
	}
	public String getIT(){
		return tf_it.getText();
	}
	public String getET(){
		return cb_et.getSelectedItem().toString();
	}
	public String getWST(){
		if(wst.getValue().equals(0)){
			return "Nil";
		}
		else{
			String time=wst.getValue().toString();
			String time2=cb_wst.getSelectedItem().toString();
			return time + time2;
		}
	}
	
	public String getWET(){
		if(wet.getValue().equals(0)){
			return "Nil";
		}
		else{
			String time=wet.getValue().toString();
			String time2=cb_wet.getSelectedItem().toString();
			return time + time2;
		}
	}
	
	public double getSalary(){
	//	int phonenum = Integer.parseInt(tb_phone.getText());
		 
     //   return phonenum;
			double amt = Double.parseDouble(tf_salary.getText());
			//double salary =  Double.parseDouble(salary.getValue());
	        return amt;
	}
	
	public String getBenefit(){
		return tf_benefit.getText();
	}
	public String getCName(){
		return tf_cname.getText();
	}
	public int getCID(){
		return CompanyApp.getCurrentUser().getCompanyId();
	}

	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JobCreateJobTest frame = new JobCreateJobTest();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
*/
	/**
	 * Create the frame.
	 */
	public JobCreateJob() {
		setTitle("Company - Create Job Advertisement ");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 740, 560);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);	
		getContentPane().setLayout(null);
		contentPane.setLayout(null);
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		
		lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 35));
		lblNewLabel.setBounds(0, 38, 724, 44);
		getContentPane().add(lblNewLabel);
		
		JLabel lblJobTitle = new JLabel("Job Title:");
		lblJobTitle.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblJobTitle.setBounds(73, 93, 142, 31);
		getContentPane().add(lblJobTitle);
		
		JLabel lblNewLabel_1 = new JLabel("Description:");
		lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblNewLabel_1.setBounds(73, 169, 142, 31);
		getContentPane().add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("Requirement:");
		lblNewLabel_2.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblNewLabel_2.setBounds(73, 206, 142, 31);
		getContentPane().add(lblNewLabel_2);
		
		JLabel lblNewLabel_3 = new JLabel("Duty:");
		lblNewLabel_3.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblNewLabel_3.setBounds(73, 243, 142, 31);
		getContentPane().add(lblNewLabel_3);
		
		JLabel lblNewLabel_4 = new JLabel("Industry Type:");
		lblNewLabel_4.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblNewLabel_4.setBounds(73, 280, 142, 31);
		getContentPane().add(lblNewLabel_4);
		
		JLabel lblNewLabel_5 = new JLabel("Employment Type:");
		lblNewLabel_5.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblNewLabel_5.setBounds(73, 317, 142, 31);
		getContentPane().add(lblNewLabel_5);
		
		JLabel lblNewLabel_6 = new JLabel("Working Hours:");
		lblNewLabel_6.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblNewLabel_6.setBounds(73, 356, 142, 31);
		getContentPane().add(lblNewLabel_6);
		
		JLabel lblSalary = new JLabel("Salary(SGD($)):");
		lblSalary.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblSalary.setBounds(73, 391, 142, 31);
		getContentPane().add(lblSalary);
		
		JLabel lblBenefit = new JLabel("Benefit:");
		lblBenefit.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblBenefit.setBounds(73, 427, 142, 31);
		getContentPane().add(lblBenefit);
		
		JLabel lblNewLabel_7 = new JLabel("Company Name:");
		lblNewLabel_7.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblNewLabel_7.setBounds(73, 132, 142, 31);
		getContentPane().add(lblNewLabel_7);
		
		tf_title = new JTextField();
		tf_title.setBounds(225, 97, 358, 26);
		getContentPane().add(tf_title);
		tf_title.setColumns(10);
		
		tf_desc = new JTextField();
		tf_desc.setBounds(225, 173, 471, 26);
		getContentPane().add(tf_desc);
		tf_desc.setColumns(10);
		
		tf_req = new JTextField();
		tf_req.setBounds(225, 210, 471, 26);
		getContentPane().add(tf_req);
		tf_req.setColumns(10);
		
		tf_duty = new JTextField();
		tf_duty.setBounds(225, 247, 471, 26);
		getContentPane().add(tf_duty);
		tf_duty.setColumns(10);
		
		tf_it = new JTextField();
		tf_it.setBounds(225, 284, 209, 26);
		getContentPane().add(tf_it);
		tf_it.setColumns(10);
		
		cb_et = new JComboBox();
		cb_et.setModel(new DefaultComboBoxModel(new String[] {"Full-time", "Part-time"}));
		cb_et.setBounds(225, 321, 108, 26);
		getContentPane().add(cb_et);
		
		JLabel lblTo = new JLabel("To");
		lblTo.setHorizontalAlignment(SwingConstants.CENTER);
		lblTo.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblTo.setBounds(392, 358, 29, 26);
		getContentPane().add(lblTo);
		
		wst = new JSpinner();
		wst.setModel(new SpinnerNumberModel(0, 0, 12, 1));
		wst.setBounds(225, 361, 120, 24);
		getContentPane().add(wst);
		
		cb_wst = new JComboBox();
		cb_wst.setModel(new DefaultComboBoxModel(new String[] {"am", "pm"}));
		cb_wst.setBounds(345, 362, 51, 23);
		getContentPane().add(cb_wst);
		
		wet = new JSpinner();
		wet.setModel(new SpinnerNumberModel(0, 0, 12, 1));
		wet.setBounds(417, 361, 116, 24);
		getContentPane().add(wet);
		
		cb_wet = new JComboBox();
		cb_wet.setModel(new DefaultComboBoxModel(new String[] {"pm", "am"}));
		cb_wet.setBounds(532, 362, 51, 23);
		getContentPane().add(cb_wet);
		
		tf_benefit = new JTextField();
		tf_benefit.setBounds(225, 431, 471, 26);
		getContentPane().add(tf_benefit);
		tf_benefit.setColumns(10);
		
		tf_cname = new JTextField();
		tf_cname.setEditable(false);
		tf_cname.setText(CompanyApp.getCurrentUser().getCompanyName());
		tf_cname.setBounds(225, 136, 292, 26);
		getContentPane().add(tf_cname);
		tf_cname.setColumns(10);
		
		JButton btnPost = new JButton("Post");
		btnPost.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				 if(tf_title.getText().equals("") || tf_desc.getText().equals("") ||tf_req.getText().equals("") || tf_duty.getText().equals("") || tf_it.getText().equals("")||tf_benefit.getText().equals("")){ 
                     JOptionPane.showMessageDialog(null,"Please fill in all the necessary details.");
				 }
				 else{
					 
					 jct.CreateJob();
				 }
			}
		});
		btnPost.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnPost.setBounds(225, 468, 150, 31);
		getContentPane().add(btnPost);
		
	/*	salary = new JSpinner();
		salary.setModel(new SpinnerNumberModel(0.0, 0.0, 50000.0, 1.0));
		salary.setBounds(225, 357, 358, 26);
		getContentPane().add(salary);
		*/
		tf_salary = new JTextField();
	//	tf_salary.setText("0");
		tf_salary.setBounds(225, 396, 86, 24);
		contentPane.add(tf_salary);
		tf_salary.setColumns(10);
		
		JButton btnClear = new JButton("Reset");
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				 tf_title.setText("");
				 tf_desc.setText("");
				 tf_req.setText("");
				 tf_duty.setText("");
				 tf_it.setText("");
				 cb_et.setSelectedItem("Full-time");
				 wst.setValue(0);
				 cb_wst.setSelectedItem("am");
				 wet.setValue(0);
				 cb_wet.setSelectedItem("pm");
				 tf_benefit.setText("");	
				 tf_salary.setText("");
			}
		});
		btnClear.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnClear.setBounds(388, 468, 150, 31);
		contentPane.add(btnClear);
		
		
		
		
		
		menuBar = new JMenuBar();
		menuBar.setBounds(0, 0, 724, 24);
		contentPane.add(menuBar);
		
		menuHome = new JMenu("Home");
		menuBar.add(menuHome);
		menuhomescreen=new JMenuItem("Home Screen");
		menuhomescreen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CompanyHomePage chp=new CompanyHomePage();
				chp.setLocationRelativeTo(null);
				chp.setVisible(true);
				chp.setTimer("start");
				chp.setTitle("Company - Home Screen");
				closeWindow();
			}
		});
		menuHome.add(menuhomescreen);
		
		menuJobAd=new JMenu("Job Advertisement");
		menuBar.add(menuJobAd);
		menuCreateJob=new JMenuItem("Create Job Advertisement");
		menuCreateJob.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JobCreateJob jcj=new JobCreateJob();
				jcj.setLocationRelativeTo(null);
				jcj.setVisible(true);
				jcj.setTitle("Company - Create Job Advertisement ");
				closeWindow();
			}
		});
		menuJobAd.add(menuCreateJob);
		
		JSeparator separator_1 = new JSeparator();
		menuJobAd.add(separator_1);
		menuRemoveJob=new JMenuItem("Remove Job Advertisement");
		menuRemoveJob.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JobRemoveJob jrj=new JobRemoveJob();
				jrj.setLocationRelativeTo(null);
				jrj.setVisible(true);
				jrj.setTitle("Company - Remove Job Advertisement ");
				closeWindow();
			}
		});
		menuViewAllJobsByCompany=new JMenuItem("View All Jobs By Company");
		menuViewAllJobsByCompany.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CompanyViewAllJobsByCompany cvajbc=new CompanyViewAllJobsByCompany();
				cvajbc.setLocationRelativeTo(null);
				cvajbc.setVisible(true);
				cvajbc.setTitle("Company - View All Jobs By Company ");
				closeWindow();
			}
		});
		menuJobAd.add(menuViewAllJobsByCompany);
		
		JSeparator separator_2 = new JSeparator();
		menuJobAd.add(separator_2);
		menuJobAd.add(menuRemoveJob);
		
		horizontalGlue = Box.createHorizontalGlue();
		menuBar.add(horizontalGlue);
		
		
		
		lblWelcome = new JLabel();
		lblWelcome.setForeground(new Color(0, 0, 128));
		if (CompanyApp.isUserLogin())
		{
			lblWelcome.setText("Welcome, " + CompanyApp.getCurrentUser().getCompanyName() + ".");
		}
		else
		{
			lblWelcome.setText("Welcome, Guest! Please Login.");
		}
		menuBar.add(lblWelcome);
		
		horizontalStrut = Box.createHorizontalStrut(20);
		menuBar.add(horizontalStrut);
		
		mnProfile = new JMenu("Profile");
		menuBar.add(mnProfile);
		
		mntmUpdateCompany = new JMenuItem("Update Company");
		mntmUpdateCompany.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CompanyUpdateCompany cuc = new CompanyUpdateCompany();
				cuc.setLocationRelativeTo(null);
				cuc.setVisible(true);
				cuc.setTitle("Company - Update Company Details");
				closeWindow();
			}
		});
		
		JMenuItem mntmViewCompany = new JMenuItem("View Company");
		mntmViewCompany.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CompanyViewDetails cvd= new CompanyViewDetails();
				cvd.setLocationRelativeTo(null);
				cvd.setVisible(true);
				cvd.setTitle("Company - View Company Details");
				closeWindow();
			}
		});
		mnProfile.add(mntmViewCompany);
		
		JSeparator separator_3 = new JSeparator();
		mnProfile.add(separator_3);
		mnProfile.add(mntmUpdateCompany);
		
		
		menuExit = new JMenu("Exit");
		menuBar.add(menuExit);
		menuLogout=new JMenuItem("Logout");
		menuLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CompanyApp.logoutUser();
				
				CompanyLogin loginUI = new CompanyLogin();
				loginUI.setLocationRelativeTo(null);
				loginUI.setVisible(true);
				closeWindow();
			}
		});
		menuExit.add(menuLogout);
		
		
		
		
		
	}
	
	public void closeWindow()
	{
		this.dispose();
	}
}
